In [28]:
# The story is stored in the file "story.txt".
f = open("story.txt", "r")
story = f.read()
print(story)
In [29]:
# We can split strings into lists with the .split() method.
# If we use a space as the input to .split(), it will split based on the space.
text = "Bears are probably better than sharks, but I can't get close enough to one to be sure."
tokenized_text = text.split(" ")
tokenized_story = story.split(" ")
print(tokenized_story)
The story has been loaded into tokenized_story.
Replace all of the punctuation in each of the tokens.
You'll need to loop through tokenized_story to do so.
You'll need to use multiple replace statements, one for each punctuation character to replace.
Append the token to no_punctuation_tokens once you are done replacing characters.
Don't forget to remove newlines!
Print out no_punctuation_tokens if you want to see which types of punctuation are still in the data.
In [30]:
# We can use the .replace function to replace punctuation in a string.
text = "Who really shot John F. Kennedy?"
text = text.replace("?", "?!")
# The question mark has been replaced with ?!.
##print(text)
# We can replace strings with blank spaces, meaning that they are just removed.
text = text.replace("?", "")
# The question mark is gone now.
##print(text)
no_punctuation_tokens = []
for token in tokenized_story:
for p in [".", ",", "\n", "'", ";", "?", "!", "-", ":"]:
token = token.replace(p, "")
no_punctuation_tokens.append(token)
print(no_punctuation_tokens)
In [31]:
# We can make strings all lowercase using the .lower() method.
text = "MY CAPS LOCK IS STUCK"
text = text.lower()
# The text is much nicer to read now.
print(text)
lowercase_tokens = []
for token in no_punctuation_tokens:
lowercase_tokens.append(token.lower())
print(lowercase_tokens)
Define a function that takes degrees in fahrenheit as an input, and return degrees celsius
Use it to convert 100 degrees fahrenheit to celsius. Assign the result to celsius_100.
Use it to convert 150 degrees fahrenheit to celsius. Assign the result to celsius_150.
In [32]:
# A simple function that takes in a number of miles, and turns it into kilometers
# The input at position 0 will be put into the miles variable.
def miles_to_km(miles):
# return is a special keyword that indicates that the function will output whatever comes after it.
return miles/0.62137
# Returns the number of kilometers equivalent to one mile
print(miles_to_km(1))
# Convert a from 10 miles to kilometers
a = 10
a = miles_to_km(a)
# We can convert and assign to a different variable
b = 50
c = miles_to_km(b)
fahrenheit = 80
celsius = (fahrenheit - 32)/1.8
def f2c(f):
c = (f - 32)/1.8
return c
celsius_100 = f2c(100)
celsius_150 = f2c(150)
print(celsius_100, celsius_150)
In [33]:
def split_string(text):
return text.split(" ")
sally = "Sally sells seashells by the seashore."
# This splits the string into a list.
print(split_string(sally))
# We can assign the output of a function to a variable.
sally_tokens = split_string(sally)
lowercase_me = "I wish I was in ALL lowercase"
def to_lowercase(text):
return text.lower()
lowercased_string = to_lowercase(lowercase_me)
print(lowercased_string)
In [34]:
# Sometimes, you will have problems with your code that cause python to throw an exception.
# Don't worry, it happens to all of us many times a day.
# An exception means that the program can't run, so you'll get an error in the results view instead of the normal output.
# There are a few different types of exceptions.
# The first we'll look at is a SyntaxError.
# This means that something is typed incorrectly (statements misspelled, quotes missing, and so on)
a = ["Errors are no fun!", "But they can be fixed", "Just fix the syntax and everything will be fine"]
b = 5
for item in a:
if b == 5:
print(item)
In [35]:
a = 5
if a == 6:
print("6 is obviously the best number")
print("What's going on, guys?")
else:
print("I never liked that 6")